package com.muyi.controller;

import com.muyi.pojo.entity.Result;
import com.muyi.pojo.entity.Tag;
import com.muyi.service.TagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "标签模块")
@Slf4j
@CrossOrigin
@RequestMapping("/tag")
@RestController
public class TagController {
    @Autowired
    private TagService tagService;

    /**
     * 查询标签数据
     * @return
     */
    @ApiOperation(value = "查询全部标签")
    @GetMapping
    public Result list(){
        log.info("查询全部标签数据");
        //调用service查询标签数据
        List<Tag> tagList =  tagService.list();
        return Result.success(tagList);
    }


    /**
     * 删除标签
     * @return
     */
    @ApiOperation(value = "删除标签")
    @DeleteMapping("/{tag_id}")
    public Result delete(@PathVariable Integer tag_id){
        log.info("根据id删除标签:{}",tag_id);
        //调用service删除标签
        tagService.delete(tag_id);
        return Result.success();
    }


    /**
     * 新增标签
     * @return
     */
    @ApiOperation(value = "新增标签")
    @PostMapping
    public Result add(@RequestBody Tag tag){
        log.info("新增标签: {}" , tag);
        //调用service新增标签
        tagService.add(tag);
        return Result.success();
    }

    @ApiOperation(value = "更新标签")
    @PutMapping
    public Result update(@RequestBody Tag tag){
        log.info("更新标签信息 : {}", tag);
        tagService.update(tag);
        return Result.success();
    }

    // 根据id查询标签
    @ApiOperation(value = "根据标签id查询标签")
    @GetMapping("/{tag_id}")
    public Result getByTagId(@PathVariable Integer tag_id){
        log.info("根据id查询标签, tag_id:{}",tag_id);
        Tag tag = tagService.getByTagId(tag_id);
        return Result.success(tag);
    }

}
